/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Apr 10, 2012  12:56:25 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : SdBookContrOutbServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.SdBookContrOutb;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class SdBookContrOutbServices extends JPAService {

	
	private static final Logger log = Logger.getLogger(SdBookContrOutbServices.class);
	
	/**
	 * @param em
	 */
	public SdBookContrOutbServices(EntityManager em) {
		super(em);
		// TODO Auto-generated constructor stub
	}
	
	public SdBookContrOutb findById(String id) throws Exception {				
		
		return super.ConvertToYoureObject(super.find(id, "SdBookContrOutb.findById"));				
	}
	
	public List<SdBookContrOutb> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "SdBookContrOutb.findByInstanceNo"));				
	}
	
	public List<SdBookContrOutb> findAllIsSync(String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("SdBookContrOutb.findAllIsSync", mapWhereValue));				
	}
	
	public List<SdBookContrOutb> findByInstanceNo1(String documentNumber, String instanceNo) throws Exception {
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("documentNumber", documentNumber);
		mapWhereValue.put("instanceNo", instanceNo);
		
		return super.ConvertToYoureObject(super.find("SdBookContrOutb.findByInstanceNo1", mapWhereValue));
	}
	
	/**
	 * 
	 * @param company
	 * @param project
	 * @param promisDocType
	 * @param documentNumber
	 * @param promisDocStatus
	 * @param instanceNo
	 * @param issync
	 * @return
	 * @throws Exception
	 */
	public List<SdBookContrOutb> findByDocument1(String company, String project, String promisDocType, String documentNumber, String promisDocStatus , String instanceNo, String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company); 									log.info(" company : " + company );
		mapWhereValue.put("project", project); 									log.info(" project : " + project );
		mapWhereValue.put("promisDocType", promisDocType); 						log.info(" promisDocType : " + promisDocType );
		mapWhereValue.put("documentNumber", documentNumber); 					log.info(" documentNumber : " + documentNumber );
		mapWhereValue.put("promisDocStatus", promisDocStatus); 					log.info(" promisDocStatus : " + promisDocStatus );
		mapWhereValue.put("instanceNo", instanceNo); 							log.info(" instanceNo : " + instanceNo );
		mapWhereValue.put("issync", issync); 									log.info(" issync : " + issync );
		
		return super.ConvertToYoureObject(super.find("SdBookContrOutb.findByDocument1", mapWhereValue));				
	}
	
	/**
	 * 
	 * @param company
	 * @param project
	 * @param promisDocType
	 * @param documentNumber
	 * @param promisDocStatus
	 * @param issync
	 * @return
	 * @throws Exception
	 */
	public List<SdBookContrOutb> findByDocument(String company, String project, String promisDocType, String documentNumber, String promisDocStatus , String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company); 									log.info(" company : " + company );
		mapWhereValue.put("project", project); 									log.info(" project : " + project );
		mapWhereValue.put("promisDocType", promisDocType); 						log.info(" promisDocType : " + promisDocType );
		mapWhereValue.put("documentNumber", documentNumber); 					log.info(" documentNumber : " + documentNumber );
		mapWhereValue.put("promisDocStatus", promisDocStatus); 					log.info(" promisDocStatus : " + promisDocStatus );
		mapWhereValue.put("issync", issync); 									log.info(" issync : " + issync );
		
		return super.ConvertToYoureObject(super.find("SdBookContrOutb.findByDocument", mapWhereValue));				
	}
	
	/**
	 * 
	 * @param company
	 * @param project
	 * @param promisDocType
	 * @param promisDocStatus
	 * @param issync
	 * @return
	 * @throws Exception
	 */
	public List<SdBookContrOutb> findByDocumentType(String company, String project, String promisDocType, String promisDocStatus , String issync) throws Exception {		
		
		Map<String,Object> mapWhereValue = new HashMap<String, Object>();
		mapWhereValue.put("company", company);
		mapWhereValue.put("project", project);
		mapWhereValue.put("promisDocType", promisDocType);
		mapWhereValue.put("promisDocStatus", promisDocStatus);
		mapWhereValue.put("issync", issync);
		
		return super.ConvertToYoureObject(super.find("SdBookContrOutb.findByDocumentType", mapWhereValue));				
	}
	
	/**
	 * 
	 * @param value
	 * @return
	 * @throws Exception
	 */
	public SdBookContrOutb Updated(SdBookContrOutb value) throws Exception {
		if(value != null)
		{
			return (SdBookContrOutb) super.updated(value);
		}else
			return null;
	}
	
	/**
	 * 
	 * @param vlaue
	 * @return
	 * @throws Exception
	 */
	public SdBookContrOutb InsertedOrUpdated(SdBookContrOutb vlaue) throws Exception {
		SdBookContrOutb obj = vlaue;
		try{		
			if(CommonService.IsNull(obj.getPromisStagingId()))
			{
				String id = super.SequenceNext("SD_BOOK_CONTR_OUTB");					
				obj.setPromisStagingId(id);							
				obj.setSyncCreatedby(this.getUser());
				obj.setSyncCreatedon(this.getCurrent_date());
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				obj.setIsactive("Y");
				obj.setIssync("N");
				super.inserted(obj);
			}else{
				
				obj.setSyncUpdatedby(this.getUser());
				obj.setSyncUpdatedon(this.getCurrent_date());
				super.updated(obj);
			}
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return obj;
	}
	
	/**
	 * 
	 * @param vlauelist
	 * @return
	 * @throws Exception
	 */
	public List<SdBookContrOutb> Inserted(List<SdBookContrOutb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SdBookContrOutb obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("SD_BOOK_CONTR_OUTB");					
					obj.setPromisStagingId(id);								
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					common_lst.add(obj);
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}
	
	/**
	 * 
	 * @param vlauelist
	 * @return
	 * @throws Exception
	 */
	public List<SdBookContrOutb> Updated(List<SdBookContrOutb> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SdBookContrOutb obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());					
					common_lst.add(obj);					
				}
			}
			
			if(common_lst != null && common_lst.size() >= 1)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			log.error(e);
			throw e;
		}
		return vlauelist;
	}


}
